<template>
  <div class="dashboard-container">
    <component :is="currentRole" />
    <div class="inputText" style="margin-top: 10px;text-align: center;">
      <el-input v-model="name" placeholder="输入产品名称或条形码查询" style="width:400px" />
      <el-button type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="search">搜索</el-button>
      <div style="margin-top: 20px;">
        {{ shelf.msg }}
      </div>
    </div>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import adminDashboard from './admin'
import editorDashboard from './editor'
import { getBarCodeOrName } from '@/api/user'

export default {
  name: 'Dashboard',
  components: { adminDashboard, editorDashboard },
  data() {
    return {
      currentRole: 'adminDashboard',
      name: '',
      shelf: {}
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ])
  },
  watch: {
    // 监听搜索框内数据
    name() {
      this.shelf.msg = ''
    }
  },
  created() {
    if (!this.roles.includes('admin')) {
      this.currentRole = 'editorDashboard'
    }
  },
  methods: {

    search() {
      getBarCodeOrName(this.name).then(response => {
        this.shelf = response.obj
        console.log(this.shelf)
      })
    }

  }
}
</script>
